package iris_util

import (
	stdContext "context"
	"gitee.com/xlizy/common-go/base/response"
	"gitee.com/xlizy/common-go/utils/middleware"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
	"time"
)

var _app *iris.Application

func GetDefaultApp() *iris.Application {
	_app = iris.New()
	//优先级UseRouter>Use;
	_app.UseRouter(middleware.GlobalRouterHandler)
	_app.Use(middleware.RouteLogHandler)
	_app.Use(middleware.SaveOpHandler)
	_app.Get("/actuator/health", func(ctx *context.Context) {
		_ = ctx.JSON(response.Succ())
	})
	return _app
}

// GracefulShutdown 优雅关闭
func GracefulShutdown() {
	if _app != nil {
		ctx, cancel := stdContext.WithTimeout(stdContext.Background(), 15*time.Second)
		defer cancel()
		_ = _app.Shutdown(ctx)
	}
}
